function enable(amount) {
    var success = $('.has-success').length
    if (success == amount) {
        $('#sub').removeAttr('disabled').removeClass('disabled')
    } else {
        $("#sub").attr('disabled', 'disabled')
        $('#sub').addClass('disabled')
    }
}

function check(rule, object, length, checkfunction) {
    $(object).bind('input propertychange', function (e) {
        e.preventDefault();
        switch (rule) {
            case 'number':
                fix = /^[0-9]*$/;
                break;
            case 'all':
                fix = /^[A-Za-z0-9]+$/;
                break;
        }
        var value = $(this).val()
        var match = fix.test(value)
        var actlength = value.length
        if (!match || value == '' || value == null || actlength != length) {
            $(this).parent().removeClass('has-success')
            $(this).parent().addClass('has-error')
            enable(1)
        } else {
            $(this).parent().removeClass('has-error')
            $(this).parent().addClass('has-success')
            enable(1)
        }

    });
}

function checkImmeditely(rule, object, length, checkfunction) {
    switch (rule) {
        case 'number':
            fix = /^[0-9]*$/;
            break;
        case 'all':
            fix = /^[A-Za-z0-9]+$/;
            break;
    }
    var value = $(object).val()
    var match = fix.test(value)
    var actlength = value.length
    if (!match || value == '' || value == null || actlength != length) {
        $(object).parent().removeClass('has-success')
        $(object).parent().addClass('has-error')
        enable(1)
    } else {
        $(object).parent().removeClass('has-error')
        $(object).parent().addClass('has-success')
        enable(1)
    }
}

function random(number) {
    let raw = Math.random()
    let baies = Math.pow(10, number)
    return Math.ceil(raw * baies)
}
check('number', '#input_amount', 6)
check('number', '#form-id')
$("#random").click(function (e) {
    e.preventDefault();
    $("#form-id").val(random(6));
    checkImmeditely('number', '#form-id', 6)
    return false
});
input = 0
radio = 0
checkbox = 0
form_id=$('input')[0]
$('#input').click(function (e) {
    e.preventDefault();
    $('#append').append("<div class='form-group'><label class='lab' for='input" + input + "'>请编辑标签</label><div class='input-group'><input type='text' class='form-control  input-lg input-create' id='input" + input + "' placeholder='点击编辑输入框提示内容' name='请编辑标签'><span class='input-group-addon control'><button class='btn btn-primary edit-label'>编辑标签</button></span></div></div>");
    input++
});
$('#choice').click(function (e) {
    e.preventDefault();
    var temp = radio
    $('#append').append("<div style='border: 1px dashed lightgrey;padding:10px'><span class='lab'>请编辑标题</span><button  class='btn btn-mini edit-radio-tittle control'>编辑标题</button><button attr='" + temp + "' class='btn btn-primary btn-mini add-radio control disabled' disabled>增加选项</button><br></div>");
    radio++
});
$('#muti-choice').click(function (e) {
    e.preventDefault();
    var temp = checkbox
    $('#append').append("<div style='border: 1px dashed lightgrey;padding:10px'><span class='lab'>请编辑标题</span><button class='btn btn-mini edit-checkbox-tittle control'>编辑标题</button><button attr='" + temp + "' class='btn btn-primary btn-mini add-checkbox control diabled' disabled>增加选项</button><br></div>");
    checkbox++
});
$('body').on("blur", '#append :input', function () {
    var value = $(this).val();
    $(this).attr('placeholder', value);
});
$('body').on("click", '.edit-label', function (e) {
    e.preventDefault();
    $(this).hide();
    $(this).parent().prepend("<input type='text' class='control'>&nbsp;<button class='btn control' id='temp'type='button'>确定</button>&nbsp;");
});
$('body').on("click", '.edit-radio', function (e) {
    e.preventDefault();
    $(this).hide();
    $(this).before("<input type='text' class='temp control'>&nbsp;<button class='btn btn-mini control' id='tempr'type='button'>确定</button>&nbsp;");
});
$('body').on("click", '.edit-checkbox', function (e) {
    e.preventDefault();
    $(this).hide();
    $(this).before("<input type='text' class='temp control'>&nbsp;<button class='btn btn-mini control' id='tempr'type='button'>确定</button>&nbsp;");
});
$('body').on("click", '.edit-radio-tittle', function (e) {
    e.preventDefault();
    $(this).hide();
    $(this).before("<input type='text' class='temp control'>&nbsp;<button class='btn btn-mini control' id='temprt'type='button'>确定</button>&nbsp;");
});
$('body').on("click", '.edit-checkbox-tittle', function (e) {
    e.preventDefault();
    $(this).hide();
    $(this).before("<input type='text' class='temp control'>&nbsp;<button class='btn btn-mini control' id='temprt'type='button'>确定</button>&nbsp;");
});
$('body').on("click", '.add-radio', function (e) {
    e.preventDefault();
    var temp = $(this).attr("attr");
    $(this).parent().append("<label class='radio-inline'><input type='radio' name='" + temp + "' value='单选框'><span>单选框</span><button class='btn btn-mini btn-primary edit-radio control'>编辑选项名</button></label>");
});
$('body').on("click", '.add-checkbox', function (e) {
    e.preventDefault();
    var temp = $(this).attr("attr");
    $(this).parent().append("<label class='checkbox-inline'><input type='checkbox' name='" + temp + "' value='复选框' ><span>复选框</span><button class='btn btn-mini btn-primary edit-checkbox control'>编辑选项名</button></label>");
});
$('body').on("click", "#tempr", function (e) {
    e.preventDefault();
    var lable = $(this).siblings('span')
    var value = $(this).siblings('.temp').attr('placeholder');
    $(lable).html(value);
    $(this).siblings('.temp').remove();
    $(this).siblings().show()
    $(this).siblings("input").val(value);
    $(this).remove();
})
$('body').on("click", "#temprt", function (e) {
    e.preventDefault();
    var lable = $(this).siblings('span')
    var value = $(this).siblings('.temp').attr('placeholder');
    var radio=$(this).siblings('.radio-inline').children('input')
    var checkbox=$(this).siblings('.checkbox-inline').children('input')
    if(!value){
        alert("不能为空！")
        return 0
    }
    $(lable).html(value);
    $(this).siblings("button").attr('attr', value);
    $(this).siblings("button").removeAttr('disabled');
    $(this).siblings("button").removeClass('disabled');
    $(radio).attr('name', value);
    $(checkbox).attr('name', value);
    $(this).siblings('.temp').remove();
    $(this).siblings().show()
    $(this).remove();

})
$('body').on("click", "#temp", function (e) {
    e.preventDefault();
    var lable = $(this).parent().parent().siblings('.lab')
    var name = $(this).parent().siblings("input")
    var value = $(this).siblings('input').attr('placeholder');
    $(lable).html(value)
    $(name).attr('name', value);
    $(this).siblings('input').remove();
    $(this).siblings().show()
    $(this).remove();

})
preview=''
$("#sub").click(function (e) {
    var actForm = $('#form').html();
    $(".control").remove();
    $(".notice").remove();
    $('#form').find('div').css({
        'border': 'none'
    });
    preview = $('#form').html();
    $('#form').html(actForm);
    $("#form-preview").html(preview);
});
$('body').on('click', '.hide-control', function (e) {
    e.preventDefault();
    $('.control').hide();
    $(this).removeClass("hide-control")
});
$('body').on('click', '.active', function () {
    $('.control').show();
    $(this).addClass("hide-control")
});
$(".form-datetime").datetimepicker(
    {
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd hh:ii"
    });
function submit() {
    var timeStart=$("#timeStart").val();
    var timeEnd=$("#timeEnd").val();
    if(timeEnd<=timeStart){
        alert("结束时间必须大于开始时间，请修正。")
        return 0
    }
    $.ajax({
        type: "post",
        url: "../php/form_create/seralize.php",
        data: {
            input: input,
            radio: radio,
            checkbox: checkbox,
            time_start:timeStart,
            time_end:timeEnd,
            form_code: getRawArray(preview),
            form_id:$(form_id).val(),
        },
        dataType: "text",
        success: function (response) {
            alert(response)
        },
        error:function (XMLHttpRequest, textStatus, errorThrown){
            console.log(XMLDocument+textStatus+errorThrown)
        }
    });
}